<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<style type="text/css">

    body .my-skin .layui-layer-btn1{
        background-color: #FFCC33;
        border: 1px solid #FFCC33;
        color: #FFF;
    }

</style>
<head>
  <meta charset="utf-8">
  <title>开启头部工具栏 - 数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/back/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/back/style/admin.css" media="all">
</head>
<body>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>组件</cite></a>
      <a><cite>数据表格</cite></a>
      <a><cite>开启头部工具栏</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">用户列表</div>
          <div class="layui-card-body">
          	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
              <input type="text" name="loginname" placeholder="请输入" autocomplete="off" class="layui-input" id="adminName">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
              <select name="role" id="adminPuis">
              	<option value="0">请选择</option>
                <option value="1">管理员</option>
                <option value="2">超级管理员</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" id="fuzzyQuery">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
          <div class="layui-inline">
         			 <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
          </div>
        </div>
      </div>
      	
            <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table>
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
              </div>
            </script>
            
            <script type="text/html" id="buttonTpl">
                {{#  if(d.adminPuis == 2){ }}
                <button class="layui-btn layui-btn-warm layui-btn-xs">超级管理员</button>
                {{#  } else if(d.adminPuis == 1){ }}
                <button class="layui-btn layui-btn-norma layui-btn-xs">管理员</button>
                {{#  } }}
            </script>
            
            <script type="text/html" id="buttonTp2">
                {{#  if(d.adminMark == 1){ }}
                <button class="layui-btn layui-btn-xs">启用</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-danger layui-btn-xs">禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="${pageContext.request.contextPath }/back/layui/layui.js"></script>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
      function renderdata(adminName,adminPuis){
  layui.config({
    base: '${pageContext.request.contextPath }/back/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var admin = layui.admin
    ,$ = layui.$
    ,form = layui.form
    ,table = layui.table;
  
    table.render({
      elem: '#test-table-toolbar'
      ,url: '${pageContext.request.contextPath }/AdminsController/getqueryAllAdmin?adminName=' + adminName+'&adminPuis='+adminPuis
      ,toolbar: '#test-table-toolbar-toolbarDemo'
      ,title: '用户数据表'
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field:'adminId', title:'ID', width:200, fixed: 'left', unresize: true, sort: true}
        ,{field:'adminName', title:'用户名', width:200}
        ,{field:'adminPwd', title:'密码', width:200}
        ,{field: 'adminPuis', title:'管理员权限', templet: '#buttonTpl', width: 180, align: 'center',}
        ,{field: 'adminMark', title:'管理员状态', templet: '#buttonTp2', width: 180, align: 'center',}
        ,{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:200}
      ]],
        request: {pageName: 'pageNum',limitName: 'pageSize'}
        ,page: true
        ,limit:5   //默认十条数据一页
        ,limits: [3, 5, 8, 10, 15, 20]
    });
    
    //头工具栏事件
    table.on('toolbar(test-table-toolbar)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
          var data = checkStatus.data;
          layer.alert(JSON.stringify(data));
        break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
          layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
      };
    });
    
    //监听行工具事件
    table.on('tool(test-table-toolbar)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('确认删除', function(index){
         // obj.del();
            delCarouselById(data.adminId,"0");
            layer.msg('删除成功');
          layer.close(index);
        });
      } else if(obj.event === 'edit'){
          if(data.adminMark == 1){

              layer.confirm('',{title : "修改选项",skin : "my-skin",btn: ['初始化密码', '更改管理员权限']}, function(index){
                  initializeTheById(data.adminId);
                  layer.msg('初始化成功：密码：123456');
                  layer.close(index);

              }, function(index) {
                if (data.adminPuis==2){
                    changeById(data.adminId,"1")
                }else {
                    changeById(data.adminId,"2")
                }
                  layer.msg('管理员权限修改成功');
                  layer.close(index);
              });

          }else {
              layer.confirm('确认启用该用户', function(index){
                  //layer.alert(JSON.stringify(data));
                  delCarouselById(data.adminId,"1");
                  layer.msg('启用成功');
                  layer.close(index);
              });
          }
      }
      
    });
  	 
  	//事件
    var active = {
      add: function(){
        layer.open({
          type: 2
          ,title: '添加管理员'
          ,content: '${pageContext.request.contextPath }/BackController/adminform'
          ,area: ['400px', '400px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'LAY-user-back-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              table.reload('LAY-user-front-submit'); //数据刷新
              layer.close(index); //关闭弹层
            });  
            
            submit.trigger('click');
          }
        }); 
      }
    }  
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
      }

      //删除记录(修改状态）
      function delCarouselById(adminId,adminMark) {
          $.get("${pageContext.request.contextPath }/AdminsController/modifyAdminMark?adminId=" + adminId+"&adminMark="+adminMark,
              function(data, status) {
                  $(".layui-laypage-btn").click() ;//刷新当前行
              });
      }
      //初始化密码
      function initializeTheById(adminId) {
          $.get("${pageContext.request.contextPath }/AdminsController/InitializeTheAdminPwd?adminId=" + adminId,
              function(data, status) {
                  $(".layui-laypage-btn").click() ;//刷新当前行
              });
      }
      //更改管理员权限
      function changeById(adminId,adminPuis) {
          $.get("${pageContext.request.contextPath }/AdminsController/changeAdminPuis?adminId=" + adminId+"&adminPuis="+adminPuis,
              function(data, status) {
                  $(".layui-laypage-btn").click() ;//刷新当前行
              });
      }
      $(function() {
          //渲染数据
          renderdata("",0);

          //模糊查询
          $("#fuzzyQuery").click(function(){
              var adminName=$("#adminName").val();
              var adminPuis=$("#adminPuis").val();

              renderdata(adminName,adminPuis);
          })

      })
  </script>
</body>
</html>